package com.sc.outpatientsys.service.impl;

import com.sc.outpatientsys.entity.Diagnosis;
import com.sc.outpatientsys.mapper.DiagnosisMapper;
import com.sc.outpatientsys.service.IDiagnosisService;
import com.sc.outpatientsys.service.ex.DeleteException;
import com.sc.outpatientsys.service.ex.InsertException;
import com.sc.outpatientsys.service.ex.NotFindException;
import com.sc.outpatientsys.service.ex.UpdateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@Slf4j
public class DiagnosisServiceImpl implements IDiagnosisService {
    @Autowired
    private DiagnosisMapper diagnosisMapper;

    @Override
    public void insertDiagnosis(Diagnosis diagnosis) {
        log.info("Service:"+diagnosis.toString());

        int rows = diagnosisMapper.insertDiagnosis(diagnosis);
//        log.info("Service:"+diagnosis.toString());
        if(rows!=1){
            throw new InsertException("添加诊断信息错误");
        }
    }

    @Override
    public Diagnosis findById(int dg_ID) {
        Diagnosis rst = diagnosisMapper.findDiagnosisByID(dg_ID);
        if(rst==null){
            throw new NotFindException("未找到");
        }
        return rst;
    }

    @Override
    public Diagnosis findByCR_ID(int cr_ID) {
        Diagnosis rst = diagnosisMapper.findDiagnosisByCR_ID(cr_ID);
        if(rst == null){
            throw new NotFindException("未找到");
        }
        return rst;
    }

    @Override
    public List<Diagnosis> findAll() {
        List<Diagnosis> rst = diagnosisMapper.findAll();
        if(rst == null){
            throw new NotFindException("未找到");
        }
        return rst;
    }

    @Override
    public void update(Diagnosis diagnosis) {
        int rst = diagnosisMapper.updateDiagnosis(diagnosis);
        if(rst!=1){
            throw new UpdateException("修改时发生错误");
        }
    }

    @Override
    public void update_List(String med_ID_List,int cr_ID) {
        int rst = diagnosisMapper.updateDiagnosis_MedList(med_ID_List,cr_ID);
        if (rst != 1){
            throw new UpdateException("修改时发生错误");
        }
    }

    @Override
    public void delete(int cr_ID) {
        int rst = diagnosisMapper.deleteDiagnosis(cr_ID);
        if (rst!=1){
            throw new DeleteException("删除时发生错误");
        }
    }

    @Override
    public void deleteByDg_Id(int dg_ID) {
        int rst = diagnosisMapper.deleteDiagnosisByDg_Id(dg_ID);
        if(rst!=1){
            throw new DeleteException("删除时发生错误");
        }
    }

}
